<template>
  <!--<el-table
    :data="tableData"
    stripe
    style="width: 100%">
    <el-table-column
      prop="username"
      label="日期"
      width="180">
    </el-table-column>
    <el-table-column
      prop="name"
      label="姓名"
      width="180">
    </el-table-column>
    <el-table-column
      prop="address"
      label="地址">
    </el-table-column>
  </el-table>-->
  <div id="users">
   <!-- <el-breadcrumb separator="/" style="padding-left:10px;padding-bottom:10px;font-size:12px;">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>系统管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
    </el-breadcrumb>-->
    <!-- 用户列表卡片区 -->
    <el-card class="box-card">
      <el-form :inline="true" ref="form" :model="queryMap" label-width="70px">
        <el-form-item label="部门">
          <el-select
            clearable
            @change="searchUser"
            @clear="searchUser"
            v-model="queryMap.departmentId"
            placeholder="请选择所属部门">
            <el-option
              v-for="department in departments"
              :label="department.name"
              :key="department.id"
              :value="department.id"
            >
              <span style="float: left">{{ department.name }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">
                <el-tag size="mini" effect="plain" type="success">
                  {{ department.deptCount }}人
                </el-tag>
              </span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户名">
          <el-input
            @keyup.enter.native="searchUser"
            @clear="searchUser"
            clearable
            v-model="queryMap.username"
            placeholder="请输入用户名查询"
          ></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input
            @keyup.enter.native="searchUser"
            clearable
            @clear="searchUser"
            v-model="queryMap.email"
            placeholder="请输入邮箱查询"
          ></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-radio v-model="queryMap.sex" label="1">男</el-radio>
          <el-radio v-model="queryMap.sex" label="0">女</el-radio>
          <el-radio v-model="queryMap.sex" label>全部</el-radio>
        </el-form-item>
        <el-form-item label="昵称">
          <el-input clearable @clear="searchUser" v-model="queryMap.nickname" placeholder="请输入昵称查询"></el-input>
        </el-form-item>
        <!-- <el-form-item label="状态">
          <el-select
            clearable
            v-model="queryMap.isban"
            @clear="searchUser"
            placeholder="请选择用户状态"
          >
            <el-option label="全部" value=""></el-option>
            <el-option label="禁用" value="1"></el-option>
            <el-option label="正常" value="0"></el-option>
          </el-select>
        </el-form-item>-->

        <el-form-item style="margin-left:50px;">
          <el-button  @click="reset" icon="el-icon-refresh">重置</el-button>
          <el-button type="primary" @click="searchUser" icon="el-icon-search">查询</el-button>
          <el-button
            type="success"
            icon="el-icon-plus"
            @click="addDialogVisible=true"
            v-hasPermission="'user:add'"
          >添加</el-button>
          <el-button @click="downExcel" v-hasPermission="'user:export'"  icon="el-icon-download">导出</el-button>
        </el-form-item>
      </el-form>

      <!-- 表格区域 -->
      <el-table :data="userList" border style="width: 100%;" height="390">
        <!-- <el-table-column type="selection" width="40"></el-table-column> -->
        <el-table-column label="#" prop="id" width="50"></el-table-column>
        <el-table-column prop="username" label="用户名" width="110"></el-table-column>
        <el-table-column prop="sex" :formatter="showSex" label="性别" width="100">
          <template slot-scope="scope">
            <el-tag size="mini" type="success" v-if="scope.row.sex==1">男</el-tag>
            <el-tag size="mini"  type="warning" v-else>女</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="所属部门" width="180" sortable></el-table-column>
        <el-table-column prop="birth" label="生日" width="180" sortable></el-table-column>
        <el-table-column prop="email" label="邮箱" width="180"></el-table-column>
        <el-table-column prop="phoneNumber" label="电话" width="150"></el-table-column>
        <el-table-column prop="isban" label="是否禁用" width="100">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              @change="changUserStatus(scope.row)"
              :active-value="0"
              :inactive-value="1"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" >
          <template slot-scope="scope">
            <el-button v-hasPermission="'user:edit'" size="mini" type="primary" icon="el-icon-edit-outline" @click="edit(scope.row.id)"></el-button>
            <el-button v-hasPermission="'user:delete'" type="danger" size="mini" icon="el-icon-delete" @click="del(scope.row.id)"></el-button>
            <el-tooltip
              class="item"
              effect="dark"
              content="分配角色"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="warning"
                size="mini"
                icon="el-icon-s-tools"
                @click="assignRoles(scope.row.id)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        style="margin-top:10px;"
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryMap.current"
        :page-sizes="[5, 6, 8, 10]"
        :page-size="queryMap.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>

      <!-- 添加对话框 -->
      <el-dialog title="添加用户" @close="closeDialog" :visible.sync="addDialogVisible" width="50%;">
        <!-- 表单 -->
        <span>
          <el-form
            :model="addForm"
            :label-position="labelPosition"
            :rules="addFormRules"
            ref="addFormRef"
            label-width="80px"
          >
            <el-row>
              <el-col :span="10">
                <div class="grid-content bg-purple">
                  <el-form-item label="用户名" prop="username">
                    <el-input v-model="addForm.username"></el-input>
                  </el-form-item>
                </div>
              </el-col>
              <el-col :span="12">
                <div class="grid-content bg-purple-light">
                  <el-form-item label="部门" prop="departmentId">
                    <el-select v-model="addForm.departmentId" placeholder="请选择所属部门">
                      <el-option
                        v-for="department in departments"
                        :key="department.id"
                        :label="department.name"
                        :value="department.id"
                      ></el-option>
                    </el-select>
                  </el-form-item>
                </div>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="10">
                <div class="grid-content bg-purple">
                  <el-form-item label="昵称" prop="nickname">
                    <el-input v-model="addForm.nickname"></el-input>
                  </el-form-item>
                </div>
              </el-col>
              <el-col :span="12">
                <div class="grid-content bg-purple-light">
                  <el-form-item label="性别" prop="sex">
                    <el-radio-group v-model="addForm.sex">
                      <el-radio :label="1">男</el-radio>
                      <el-radio :label="0">女</el-radio>
                    </el-radio-group>
                  </el-form-item>
                </div>
              </el-col>
            </el-row>
            <el-form-item label="密码" prop="password">
              <el-input v-model="addForm.password" show-password></el-input>
            </el-form-item>
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="addForm.email"></el-input>
            </el-form-item>
            <el-form-item label="手机" prop="phoneNumber">
              <el-input v-model="addForm.phoneNumber"></el-input>
            </el-form-item>
            <el-form-item prop="birth" label="生日">
              <el-col :span="11">
                <el-date-picker
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="选择出生日期"
                  v-model="addForm.birth"
                  style="width: 100%;"
                ></el-date-picker>
              </el-col>
            </el-form-item>
          </el-form>
        </span>

        <span slot="footer" class="dialog-footer">
          <el-button @click="addDialogVisible = false">取 消</el-button>
          <el-button
            type="primary"
            @click="addUser"
            :loading="btnLoading"
            :disabled="btnDisabled"
          >确 定</el-button>
        </span>
      </el-dialog>
      <!-- 修改对话框 -->
      <el-dialog title="修改用户" :visible.sync="editDialogVisible" width="50%" @close="editClose">
        <span>
          <el-form
            :model="editForm"
            :label-position="labelPosition"
            :rules="addFormRules"
            ref="editFormRef"
            label-width="80px"
          >
            <el-row>
              <el-col :span="10">
                <div class="grid-content bg-purple">
                  <el-form-item label="用户名" prop="username">
                    <el-input v-model="editForm.username" :disabled="true"></el-input>
                    <el-input
                      type="hidden"
                      v-model="editForm.id"
                      :disabled="true"
                      style="display:none;"
                    ></el-input>
                  </el-form-item>
                </div>
              </el-col>
              <el-col :span="12">
                <div class="grid-content bg-purple-light">
                  <el-form-item label="部门" prop="departmentId">
                    <el-select v-model="editForm.departmentId" placeholder="请选择所属部门">
                      <el-option
                        v-for="department in departments"
                        :key="department.id"
                        :label="department.name"
                        :value="department.id"
                      ></el-option>
                    </el-select>
                  </el-form-item>
                </div>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="10">
                <div class="grid-content bg-purple">
                  <el-form-item label="昵称" prop="nickname">
                    <el-input v-model="editForm.nickname"></el-input>
                  </el-form-item>
                </div>
              </el-col>
              <el-col :span="12">
                <div class="grid-content bg-purple-light">
                  <el-form-item label="性别" prop="sex">
                    <el-radio-group v-model="editForm.sex">
                      <el-radio :label="1">男</el-radio>
                      <el-radio :label="0">女</el-radio>
                    </el-radio-group>
                  </el-form-item>
                </div>
              </el-col>
            </el-row>

            <el-form-item label="邮箱" prop="email">
              <el-input v-model="editForm.email"></el-input>
            </el-form-item>
            <el-form-item label="联系方式" prop="phoneNumber">
              <el-input v-model="editForm.phoneNumber"></el-input>
            </el-form-item>
            <el-form-item prop="birth" label="生日">
              <el-col :span="11">
                <el-date-picker
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="选择出生日期"
                  v-model="editForm.birth"
                  style="width: 100%;"
                ></el-date-picker>
              </el-col>
            </el-form-item>
          </el-form>
        </span>

        <span slot="footer" class="dialog-footer">
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button
            type="primary"
            @click="updateUser"
            :loading="btnLoading"
            :disabled="btnDisabled"
          >确 定</el-button>
        </span>
      </el-dialog>
      <!-- 分配角色对话框 -->
      <el-dialog center title="分配角色" :visible.sync="assignDialogVisible" width="49%">
        <span>
          <template>
            <el-transfer
              filterable
              :titles="['未拥有','已拥有']"
              :button-texts="['到左边', '到右边']"
              v-model="value"
              :data="roles"
            ></el-transfer>
          </template>
        </span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="assignDialogVisible = false" class="el-icon-close">取消分配</el-button>
          <el-button
            v-hasPermission="'user:assign'"
            type="primary"
            @click="doAssignRoles"
            class="el-icon-check"
            :loading="btnLoading"
            :disabled="btnDisabled"
          >确定分配</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
  import { AddUser, editUser, getList, updUser } from '../../api/table'
  import {getDept} from '../../api/table'
  import {delUser} from '../../api/table'
  import { exportExcel } from '../../api/easyExcel'

  export default {
    name: 'list',
    data() {
      var checkEmail = (rule, value, callback) => {
        const mailReg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
        if (!value) {
          return callback(new Error("邮箱不能为空"));
        }
        setTimeout(() => {
          if (mailReg.test(value)) {
            callback();
          } else {
            callback(new Error("请输入正确的邮箱格式"));
          }
        }, 100);
      };
      var checkPhone = (rule, value, callback) => {
        const phoneReg = /^1[34578]\d{9}$$/;
        if (!value) {
          return callback(new Error("电话号码不能为空"));
        }
        setTimeout(() => {
          // Number.isInteger是es6验证数字是否为整数的方法,实际输入的数字总是识别成字符串
          // 所以在前面加了一个+实现隐式转换

          if (!Number.isInteger(+value)) {
            callback(new Error("请输入数字值"));
          } else {
            if (phoneReg.test(value)) {
              callback();
            } else {
              callback(new Error("电话号码格式不正确"));
            }
          }
        }, 100);
      };
      return {
        btnLoading: false,
        btnDisabled: false,
        departments: [],
        total: 0,
        addDialogVisible: false, //添加对话框,
        editDialogVisible: false, //修改对话框
        assignDialogVisible: false, //分配角色对话框
        labelPosition: "right", //lable对齐方式
        current: 1,
        pageSize: 6,
        //查询对象
        queryMap: {
          current:1,
          pageSize:6,
          username: "",
          sex: "",
          nickname: "",
          departmentId: ""
        },
        userList: [],

        addForm: {
          username: "",
          nickname: "",
          password: "",
          email: "",
          phoneNumber: "",
          sex: "",
          birth: ""
        }, //添加表单
        editForm: {}, //更新表单
        addFormRules: {
          username: [
            { required: true, message: "请输入用户名", trigger: "blur" },
            { min: 3, max: 10, message: "长度在 3 到 10 个字符", trigger: "blur" }
          ],
          password: [
            { required: true, message: "请输入密码", trigger: "blur" },
            { min: 3, max: 12, message: "长度在 3 到 12 个字符", trigger: "blur" }
          ],
          departmentId: [
            { required: true, message: "请选择部门", trigger: "blur" }
          ],
          sex: [{ required: true, message: "请选择性别", trigger: "blur" }],
          birth: [{ required: true, message: "请填写出生日期", trigger: "blur" }],
          email: [{ required: true, validator: checkEmail, trigger: "blur" }],
          phoneNumber: [
            {
              required: true,
              message: "请输入联系方式",
              validator: checkPhone,
              trigger: "blur"
            }
          ],
          nickname: [
            { required: true, message: "请输入昵称", trigger: "blur" },
            { min: 5, max: 10, message: "长度在 5 到 10 个字符", trigger: "blur" }
          ]
        }, //添加表单验证规则
        roles: [], //角色
        value: [], //用户拥有的角色
        uid: ""
        /*tableData: []*/
      }
    },
    methods: {
      reset(){
        this.queryMap= {
          pageNum: 1,
          pageSize: 6,
          username: "",
          sex: "",
          nickname: ""
        };
      },
      /*展示*/
      getUserList(){
        var $this=this;
        getList($this.queryMap).then(res=>{
          console.log(res.data)
          this.userList=res.data.users;
          this.total=res.data.total;
        }).catch(error=>{
          console.log(error)
        })
      },
     /* 展示部门*/
      findDept(){
        getDept().then(res=>{
          console.log(res.data)
          this.departments=res.data.departmentList;
        }).catch(error=>{
          console.log(error)
        })
      },
      handleSizeChange(val) {
        this.queryMap.pageSize = val;
        this.getUserList();
      },
      handleCurrentChange(val) {
        this.queryMap.current = val;
        this.getUserList();
      },
      /*查询*/
      searchUser(){
        this.getUserList()
      },
      async del(id){
        var $this=this;
        var res= await this.$confirm("此操作会永久删除此用户，是否继续?","提示",{
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: 'warning'
        }).then(()=>{
          delUser(id).then(res=>{
            $this.$message({
              type:'info',
              message: '用户删除成功'
            });
            console.log(res);
            $this.getUserList()
            $this.findDept()
          }).catch(error=>{
            console.log(error)
          })
        }).catch(error=>{
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        })
      },
      addUser(){
        AddUser(this.addForm).then(res=>{
          this.$message({
            type:'success',
            message: '成功'
          })
          this.getUserList()
          this.findDept()
        }).catch(error=>{
          this.$message({
            type: 'error',
            message: '失败'
          })
        })
      },
      edit(id){
        editUser(id).then(res=>{
          this.editForm=res.data.user
          this.editDialogVisible=true
        }).catch(error=>{
          console.log(error)
        })
      },
      updateUser(){
        updUser(this.editForm).then(res=>{
          this.$message({
            type: 'success',
            message: '修改成功'
          })
          this.editDialogVisible=false
          this.findDept()
          this.getUserList()
        }).catch(error=>{
          console.log(error)
        })
      },
      assignRoles(id){
          this.assignDialogVisible=true;
      },
      changUserStatus(val){
        updUser(val).then(res=>{
          this.$notify({
            title:'成功',
            message: '此用户已被禁用',
            type: 'success'
          })
          this.getUserList();
          this.findDept();
        }).catch(error=>{
          console.log(error)
          this.$message({
            type:'error',
            message: '失败'
          })
        })
      },
      downExcel(){
        exportExcel(this.queryMap).then(res=>{
          let url = window.URL.createObjectURL(new Blob([res])); // 将二进制文件转化为可访问的url
          var a = document.createElement("a");
          document.body.appendChild(a);
          a.href = url;
          a.download = "用户列表.xls";
          a.click();
          window.URL.revokeObjectURL(url);
            this.$notify({
              title: '成功',
              type: 'success',
              message: '导出成功'
            })
        }).catch(error=>{
          this.$message({
            type:'error',
            message: '失败'
          })
        })
      }
    },
    created() {
      this.getUserList();
      this.findDept();
    }
  }
</script>

<style scoped>

</style>
